<?php
class category extends spController
{
	public function index() // 分类列表
	{
		$this->tpl_title = "分类管理";
		$catalogObj = spClass("lib_category");
	//	$this->results = $catalogObj->getCatalogList(); 
		$this->results = $catalogObj->getCategory(); 
		// dump($this->results);
		$this->display("admin/category_index.html");
	} 

	function write() // 撰写
	{
		$this->tpl_title = "撰写新分类";
		$this->formUrl = spUrl("category", "add");
		$this->parentId = $this->spArgs("parent");
		$catalogObj = spClass("lib_category");
		$this->results = $catalogObj->getCatalogList();
		$this->display("admin/category_write.html");
	} 

	function edit() // 修改
	{
		$this->tpl_title = "编辑分类";
		$this->formUrl = spUrl("category", "update");

		$catalogObj = spClass("lib_category");
		$this->results = $catalogObj->getCatalogList();

		$this->parentId = $this->spArgs("parent"); 
		// 这里先判断是否传入了id
		if ($id = $this->spArgs("id"))
		{ 
			// 查找表，获取信息
			$this->info = spClass("lib_category")->find(array('id' => $id));
		} 
		else
		{ 
			// 无id则直接跳转回首页
			$this->jump(spUrl("category", "index"));
		} 
		$this->display("admin/category_write.html");
	} 

	function add() // 增加分类
	{
		$cate = spClass("lib_category");
		$newrow = array(// 这里制作新增记录的值
			'parent' => $this->spArgs('parent'),
			'name' => $this->spArgs('name'),
			'slug' => $this->spArgs('slug'),
			'sort' => $this->spArgs('sort'),
			'status' => $this->spArgs('status'),
			'thumb' => $this->spArgs('thumb'),
			'description' => $this->spArgs('description'),
			);
		$id = $cate->create($newrow);
		$pid = $this->spArgs('parent');
		if ($pid > 0)
		{
			$row = $cate->find('id=' . $pid);
			$row['sub'] = $row['sub'] . $id . ',';
			$cate->update('id=' . $pid , $row);
		} 
        spClass("lib_category")->updateCategory();
		$this->success("增加分类成功！", spUrl("category", "index"));
	} 

	function update() // 更新
	{
		$cate = spClass("lib_category"); 
		// 这里先判断是否传入了id
		if ($id = $this->spArgs("id"))
		{ 
			// 执行更新
			$conditions = array("id" => $id);
			$newrow = array('parent' => $this->spArgs('parent'),
				'name' => $this->spArgs('name'),
				'slug' => $this->spArgs('slug'),
				'sort' => $this->spArgs('sort'),
				'status' => $this->spArgs('status'),
				'thumb' => $this->spArgs('thumb'),
				'description' => $this->spArgs('description'),
				);
			$cate->update($conditions, $newrow); // 更新记录
             $cate->updateCategory();
			$this->success("更新分类成功！", spUrl("category", "index"));
		} 
	} 

	function del() // 删除
	{
		// 这里先判断是否传入了id
		if ($id = $this->spArgs("id"))
		{ 
			// 执行删除
			spClass("lib_category")->delete(array('id' => $id));
             spClass("lib_category")->updateCategory();
			$this->success("删除成功！", spUrl("category", "index"));
		} 
		else
		{ 
			// 无id则直接跳转回首页
			$this->jump(spUrl("category", "index"));
		} 
	} 
} 
